define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init();

            var table1 = $("#table1");

            // 初始化表格
            table1.bootstrapTable({
                extend: {
                    index_url: 'access/index' + location.search,
                    add_url: 'access/add',
                    edit_url: 'access/edit',
                    del_url: 'access/del',
                    multi_url: 'access/multi',
                    table: 'access',
                },
                url: 'access/index' + location.search,
                pk: 'id',
                sortName: 'id',
                undefinedText:'',
                // search:false,
                showToggle:false,
                singleSelect:true,
                showExport:false,
                toolbar: "#toolbar1",
                 // 启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedNumber: 4,

                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('序号'),table: table1,width:'20px',formatter:Table.api.formatter.natureorder,operate:false},
                        {field: 'name', title: __('项目名称'),operate:'like',width:'180px'},
                        // {field: 'investment_name', title: __('投资方'),operate:'like',width:'160px'},
                        {field: 'investment_total', title: __('总投资(亿元)'), width:'70px',operate:'BETWEEN',
                            formatter:function(value,row,index) {
                                if(value == -100)
                                    return '待定';
                                else
                                    return value;
                            }
                        },
                        {field: 'carrier_area', title: __('占地面积(亩)'),width:'80px',operate:'like'},
                        {field: 'summary', title: __('项目简介'), width: '260px',operate:false,
                            formatter:function(value,row,index){
                                if(row.summary&&row.summary.length<=50){
                                    return row.summary;
                                }
                                if(row.summary&&row.summary.length>50){
                                  return row.summary.substring(0,50)+"...";
                                }
                            }
                        },

                        
                        // {field: 'join_access_time_text', title: __('参加准入例会时间'),width:'115px',operate:false, },
                        {field: 'join_access_time', 
                        title: __('参加准入例会时间'), 
                        width: "115px",
                        operate:'RANGE', 
                        addclass:'datetimerange', 
                        formatter: Table.api.formatter.datetimedate},

                        {field: 'land_time', 
                        title: __('(预计)拿地时间'), 
                        width: "115px",
                        operate:'RANGE', 
                        addclass:'datetimerange', 
                        formatter: Table.api.formatter.datetimedate},

                        {field: 'status_name', title: __('进展情况'),width:'250px',operate:false,
                            formatter:function(value,row,index){
                                if(row.status_name&&row.status_name.length<50){
                                    return row.status_name;
                                }
                                if(row.status_name&&row.status_name.length>50){
                                  return row.status_name.substring(0,50)+"...";
                                }
                            }
                        },

                        {field: 'exist_question', title: __('存在问题'),width:'250px',operate:false,
                            formatter:function(value,row,index){
                                if(row.exist_question&&row.exist_question.length<=50){
                                    return row.exist_question;
                                }
                                if(row.exist_question&&row.exist_question.length>50){
                                  return row.exist_question.substring(0,50)+"...";
                                }
                            }
                        },
                        
                        {field: 'explain', title: __('下步措施'),width:'250px',operate:false,
                            formatter:function(value,row,index){
                                if(row.explain&&row.explain.length<=50){
                                    return row.explain;
                                }
                                if(row.explain&&row.explain.length>50){
                                  return row.explain.substring(0,50)+"...";
                                }
                            }
                        },

                
                        {field: 'chargedep_names', title: __('责任单位'),operate:'like',width:'90px'},

                        {field: 'remarks', title: __('备注'),operate:'like',width:'90px',operate:false,
                            formatter:function(value,row,index){
                                if(row.remarks&&row.remarks.length<=20){
                                    return row.remarks;
                                }
                                if(row.remarks&&row.remarks.length>20){
                                  return row.remarks.substring(0,20)+"...";
                                }
                            }
                        },

                        {field: 'operate', title: __('Operate'), width:'80px',table: table1, events: Table.api.events.operate, formatter: Table.api.formatter.operate}

                        // {field: 'operate', title: __('Operate'), width:'60px', align:'center',table: table1, events: Table.api.events.operate,
                        //     buttons: [
                        //        {
                        //             name: 'detail',
                        //             icon: 'fa fa-info',
                        //             title: __('详情'),
                        //             text: '详情',
                        //             url: 'access/detail',
                        //             extend: 'data-toggle="tooltip"',
                        //             classname: 'btn btn-xs btn-info btn-addtabs'
                        //         }
                        //     ],
                        //     formatter: Table.api.formatter.buttons

                        // },

                        
                    ]
                ],
            });

            // 为表格绑定事件
            Table.api.bindevent(table1);
            Controller.api.bindeventTable(table1);
            
            var parenttable = table1.closest('.bootstrap-table');
            var options = table1.bootstrapTable('getOptions');
            var toolbar = $(options.toolbar, parenttable);
            $(toolbar).on('click', ".btn-detail", function () {
                var that = this;
                //循环弹出多个编辑框
                $.each(table1.bootstrapTable('getSelections'), function (index, row) {
                    var url = 'access/detail';
                    row = $.extend({}, row ? row : {}, {ids: row[options.pk]});
                    var url = Table.api.replaceurl(url, row, table1);
                    var icon = 'fa fa-info';
                    Backend.api.addtabs(url, '详情-'+row.name, icon);
                });
            });

        },
        add: function () {
            $(".pending").click(function(){
                var item_id =$(this).data('id');
                if(this.checked)
                {
                    $("#"+item_id).val("待定");
                    $("#"+item_id).attr('readonly','readonly');
                }
                else
                {
                    $("#"+item_id).val("");
                    require(['bootstrap-datetimepicker'], function () {
                        var options = {
                            format: 'YYYY-MM-DD HH:mm:ss',
                            icons: {
                                time: 'fa fa-clock-o',
                                date: 'fa fa-calendar',
                                up: 'fa fa-chevron-up',
                                down: 'fa fa-chevron-down',
                                previous: 'fa fa-chevron-left',
                                next: 'fa fa-chevron-right',
                                today: 'fa fa-history',
                                clear: 'fa fa-trash',
                                close: 'fa fa-remove'
                            },
                            showTodayButton: true,
                            showClose: true
                        };
                        $("#"+item_id).parent().css('position', 'relative');
                        $("#"+item_id).datetimepicker(options).on('dp.change', function (e) {
                            $(this, document).trigger("changed");
                        });
                    });
                    $("#"+item_id).removeAttr('readonly');
                }
            });
            Controller.api.bindevent();
        },
        detail: function (){
            $(".btn-print").click(function (){
                var html = $(".print").html();
                var bodyHtml = document.body.innerHTML;
                document.body.innerHTML = html;
                window.print();
                document.body.innerHTML = bodyHtml;
            });
        },
        edit: function () {
            $(".pending").click(function(){
                var item_id =$(this).data('id');
                if(this.checked)
                {
                    $("#"+item_id).val("待定");
                    $("#"+item_id).attr('readonly','readonly');
                }
                else
                {
                    $("#"+item_id).val("");
                    require(['bootstrap-datetimepicker'], function () {
                        var options = {
                            format: 'YYYY-MM-DD HH:mm:ss',
                            icons: {
                                time: 'fa fa-clock-o',
                                date: 'fa fa-calendar',
                                up: 'fa fa-chevron-up',
                                down: 'fa fa-chevron-down',
                                previous: 'fa fa-chevron-left',
                                next: 'fa fa-chevron-right',
                                today: 'fa fa-history',
                                clear: 'fa fa-trash',
                                close: 'fa fa-remove'
                            },
                            showTodayButton: true,
                            showClose: true
                        };
                        $("#"+item_id).parent().css('position', 'relative');
                        $("#"+item_id).datetimepicker(options).on('dp.change', function (e) {
                            $(this, document).trigger("changed");
                        });
                    });
                    $("#"+item_id).removeAttr('readonly');
                }
            });            
            Controller.api.bindevent();
        },
 
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            bindeventTable: function (table) {
                var parenttable = table.closest('.bootstrap-table');
                //Bootstrap-table配置
                var options = table.bootstrapTable('getOptions');
                // console.log(options);
                //Bootstrap操作区
                var toolbar = $(options.toolbar, parenttable);




                $(toolbar).on('click', ".btn-export", function () {
                    var library_id = $(this).data('library_id');
                    console.log(library_id);
                    var options = table.bootstrapTable('getOptions');
                    var search = options.queryParams({});
                    var filter = search.filter;
                    var op = search.op;
                    // console.log(filter)
                    // console.log(op)
                    if(library_id == 3)
                        url = 'access/export';
                    // console.log(url + '?filter='+filter+'&op='+op);
                    if(url)
                        location.href= url + '?filter='+filter+'&op='+op;
                });
            }
        }
    };
    return Controller;
});